﻿// For an introduction to the Navigation template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232506
(function () {
    "use strict";

    WinJS.Binding.optimizeBindingReferences = true;

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;
    var nav = WinJS.Navigation;
    var brDuplikati = 0;
    var client;

    app.addEventListener("activated", function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                // TODO: This application has been newly launched. Initialize
                // your application here.
                initialize();

                client = new WindowsAzure.MobileServiceClient("https://stickeralbum.azure-mobile.net/", "kykxDEEXBFMtYzPyBJdwTAHlXtwiNl95");

            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
            }

            if (app.sessionState.history) {
                nav.history = app.sessionState.history;
            }
            args.setPromise(WinJS.UI.processAll().then(function () {

                initAddBtn();               //GetStickers AppBar button init
                paketFlyout.initialize();
                Data.getFromLocal();        //Deserialization of local data
                Data.zemiStats();           //Getting data from a web service

                if (nav.location) {
                    nav.history.current.initialPlaceholder = true;
                    return nav.navigate(nav.location, nav.state);
                } else {
                    return nav.navigate(Application.navigator.home);
                }
            }));
        };

        WinJS.Application.onsettings = function (e) {
            e.detail.applicationcommands = { "privacyPolicy": { title: "Privacy Policy", href: "/pages/privacy/privacy.html" } };
            WinJS.UI.SettingsFlyout.populateSettings(e);
        };
    });

    app.oncheckpoint = function (args) {
        // TODO: This application is about to be suspended. Save any state
        // that needs to persist across suspensions here. If you need to 
        // complete an asynchronous operation before your application is 
        // suspended, call args.setPromise().
        app.sessionState.history = nav.history;
    };

    app.start();

    //Function for initialization of the Search Charm
    function initialize(args) {
        var searchPane = Windows.ApplicationModel.Search.SearchPane.getForCurrentView();
        searchPane.addEventListener("querysubmitted", handlingQuerySubmitted, false);
    }

    //Function for handling the search charm
    function handlingQuerySubmitted(e) {
        var igrac = e.queryText;

        //Navigates to the search results page
        //Forwards the query text (in var igrac)
        var nav = WinJS.Navigation; nav.navigate("/pages/searchedPlayers/searchedPlayers.html", { ime: igrac });
    }

    //=================================================
    //EventListener for the GetStickers AppBar button
    //It stores the objects in the tmpDuplikati List
    function initAddBtn() {
        var btnZemi = document.getElementById("btnPaket");

        btnZemi.addEventListener("click", function (e) {
            Data.tmpZalepi = [];
            paketFlyout.emptyArray();
            for (var i = 1; i <= 5; i++) {
                fullData.getRandomPicture(paketFlyout.addSticker);
            }
        });
    }

})();